package ws.jdbc.concurrent.driver.sqlserver.tds

import ws.jdbc.concurrent.driver.sqlserver.tds.rpc.UpdateValue
import static ws.jdbc.concurrent.driver.sqlserver.tds.rpc.StoredProcedures.*

/**
 * Test the execution of RPC request on several situations.
 *
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
public class TDSMessageRPCTest extends GroovyTestCase {

  TDSMessage message;
  
  void setUp() throws IOException {
    message = new TDSMessage(new Socket(serverName, port));
    message.login("", userName, password, serverName, "JDBCDriverTests");
  }

  /**
   * Test of execSpCursorOpen() method, of class TDSMessage.
   */
  void ExecSpCursorOpen() {
    println "testExecSpCursorOpen()"
    
    message.execSpCursorOpen("select 'foo' as 'bar'")
    message.sendRPCRequest((short)0, "SELECT * FROM Table1")
  }

  void testCursorOpen() {
    println "testCursorOpen()"

    def rs = message.cursorOpen("SELECT * FROM Table1", SP_CURSOROPEN_SCROLLOPT_KEYSET, SP_CURSOROPEN_CCOPT_OPTIMISTIC)
    //println rs
    //println "cursor=" + rs.cursor
    println "rows=" + message.cursorRows()
  }

  /**
   * Test of execSpCursorFetch() method, of class TDSMessage.
   */
  void ExecSpCursorFetch() {
    println "testExecSpCursorFetch()"

    def rs = message.cursorOpen("SELECT * FROM Table1", SP_CURSOROPEN_SCROLLOPT_KEYSET, SP_CURSOROPEN_CCOPT_OPTIMISTIC)
    message.execSpCursorFetch(rs.getCursor(), SP_CURSORFETCH_FETCHTYPE_FIRST, 2, 20)
  }

  /**
   * Test of cursorFetch() method, of class TDSMessage.
   */
  void CursorFetch() {
    println "testCursorFetch()"

    def rs1 = message.cursorOpen("SELECT * FROM Table1", SP_CURSOROPEN_SCROLLOPT_KEYSET, SP_CURSOROPEN_CCOPT_OPTIMISTIC)
    println message.cursorFetch(rs1.getCursor(),  SP_CURSORFETCH_FETCHTYPE_NEXT, 1, 2)
    println message.cursorFetch(rs1.getCursor(),  SP_CURSORFETCH_FETCHTYPE_ABSOLUTE, 48, 2)
  }

  /**
   * Test of execSpCursor() method, of class TDSMessage.
   *
   * UPDATE.
   */
  void ExecSpCursorUpdate() {
    println "testExecSpCursorUpdate()"

    /* Create some update values. */
    def values = new LinkedList<UpdateValue>();
    values.add(new UpdateValue("Col1", 3))
    values.add(new UpdateValue("Col2", "3"))

    def rs = message.cursorOpen("SELECT * FROM Table1", SP_CURSOROPEN_SCROLLOPT_KEYSET, SP_CURSOROPEN_CCOPT_OPTIMISTIC)
    message.execSpCursorFetch(rs.getCursor(), SP_CURSORFETCH_FETCHTYPE_NEXT, 50, 20)
    message.execSpCursor(rs.getCursor(), SP_CURSOR_OPTYPE_UPDATE, 50, "", values)
  }

  /**
   * Test of execSpCursor() method, of class TDSMessage.
   *
   * INSERT.
   */
  void ExecSpCursorInsert() {
    println "testExecSpCursorInsert()"

    /* Create some update values. */
    def values = new LinkedList<UpdateValue>();
    values.add(new UpdateValue("Col1", 100))
    values.add(new UpdateValue("Col2", "170"))

    def rs = message.cursorOpen("SELECT * FROM Table1")
    message.execSpCursorFetch(rs.getCursor(), SP_CURSORFETCH_FETCHTYPE_FIRST, 2, 20)
    message.execSpCursor(rs.getCursor(), SP_CURSOR_OPTYPE_INSERT, 0, "", values)
  }
}